import _fetch from '@/Utils/Fetch';
import {
  mallManagement
} from '@/Utils/Urls';

// 导出资料
export const downloadTemplate = () => new Promise((resolve, reject) => {
  _fetch({
    url: mallManagement.downloadTemplate,
    type: "get",
    responseType: "blob"
  }).then(res => {
    if (res) {
      const blob = res.data;
      const urlObject = window.URL || window.webkitURL || window
      const export_blob = new Blob([blob], {
        type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
      })
      const a = document.createElement('a') // 利用a标签特性下载
      a.style.display = 'none';
      const url = urlObject.createObjectURL(export_blob);
      a.href = url;
      a.setAttribute('download', '模板.xlsx')
      document.body.appendChild(a);
      a.click();
      a.remove();
      resolve()
    } else {
      reject()
    }
  })
})

export const uploadBooks = (file) => new Promise((resolve, reject) => {
  const formData = new FormData()
  formData.append('file', file)
  _fetch({
    url: mallManagement.uploadBooks,
    type: "post",
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data"
    },
  }).then(res => {
    resolve(res)
  })
})

export const reshowBook = (id) => {
  return _fetch({
    url: mallManagement.reshowBook + `/${id}`,
    type: 'post',
  })
}

export const getBookDetail = (id) => {
  return _fetch({
    url: mallManagement.getBookDetail + `/${id}`,
    type: 'post',
    isJson: true
  })
}